/**
 * 项目名称：sipcall
 * 类 名 称：CommonUtils
 * 类 描 述：(描述信息)
 * 创 建 人：Administrator
 * 创建时间：2014-12-8 下午1:26:12
 * 修 改 人：Administrator
 * 修改时间：2014-12-8 下午1:26:12
 * 修改备注：
 * @version
 * 
*/
package com.yinhai.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.TreeMap;

import android.content.Context;
import android.graphics.Color;
import android.os.Environment;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @包名：com.mobile.voip.demo.utils
 * @类名：CommonUtils
 * @描述：(描述这个类的作用)
 * @作者：Administrator
 * @时间：2014-12-8下午1:26:12
 * @版本：1.0.0
 * 
 */
public class CommonUtils {
	/**
	 * 检测Sdcard是否存在
	 * 
	 * @return
	 */
	public static boolean isExitsSdcard() {
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			return true;
		else
			return false;
	}
	
	/**
	 * 
	 * @方法名：getDataDir
	 * @描述：(存放外部数据的根目录)
	 * @param context
	 * @return
	 * @输出：String
	 * @作者：Administrator
	 *
	 */
	public static String getDataDir(Context context) {
        String state = Environment.getExternalStorageState();
        String dir = null;
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            dir = Environment.getExternalStorageDirectory().getAbsolutePath();
        } else {
            dir = context.getFilesDir().getAbsolutePath();
        }
        return dir;
    }
	
	public static int dip2px(Context context, float dpValue) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (dpValue * scale + 0.5f);
	}
	
	public static int converToAscii(int keycode){
		   int result_code=0;
		   switch(keycode){
			   case KeyEvent.KEYCODE_0:
				    result_code=48;
				    break;
			   case KeyEvent.KEYCODE_1:
				    result_code=49;
				    break;
			   case KeyEvent.KEYCODE_2:
				    result_code=50;
				    break;
			   case KeyEvent.KEYCODE_3:
				    result_code=51;
				    break;
			   case KeyEvent.KEYCODE_4:
				    result_code=52;
				    break;
			   case KeyEvent.KEYCODE_5:
				    result_code=53;
				    break;
			   case KeyEvent.KEYCODE_6:
				    result_code=54;
				    break;
			   case KeyEvent.KEYCODE_7:
				    result_code=55;
				    break;
			   case KeyEvent.KEYCODE_8:
				    result_code=56;
				    break;
			   case KeyEvent.KEYCODE_9:
				    result_code=57;
				    break;
			   case KeyEvent.KEYCODE_STAR:   //*
				    result_code=42;
				    break;
			   case KeyEvent.KEYCODE_POUND:  //#
				    result_code=35;
				    break;
		   }
		   
		   return result_code;
	}
	
	public static void highlight(TextView tv,  String key) {
        int index = -1;
        String str = tv.getText().toString().trim();
        index = str.indexOf(key);
        if (index != -1) {
            SpannableStringBuilder style = new SpannableStringBuilder(str);
            style.setSpan(new ForegroundColorSpan(Color.GREEN), index, index+key.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            tv.setText(style);
        }
    }
	
	public static void toastShow(Context context,String str)
    {
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
    }
	
}
